package com.example.alldemo.controller;

import com.example.alldemo.entity.ex;
import com.example.alldemo.entity.show;
import com.example.alldemo.log.Log;
import com.example.alldemo.service.TestService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.tags.Param;

import javax.servlet.annotation.WebFilter;
import java.sql.SQLOutput;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@WebFilter("/*")
public class TestController {
    private static final Logger LOG = LoggerFactory.getLogger(TestController.class);

    @Autowired
    private TestService testService;
    public int temp=0;

    //用于请求时间测试
    @RequestMapping(value = "/getall")
    @ResponseBody
    public List<ex> getAll(){
        LOG.info("用户使用getall方法");
        temp++;
        System.out.println("请求次数："+temp);
        List<ex> all = testService.getAll();
        return all;
    }

    @ApiOperation("获取信息")
    @RequestMapping(value = "/get")
    public ModelAndView gethtml(Model model){
        return  new ModelAndView("index");
    }

    @RequestMapping("/err")
    public ModelAndView testError(Integer num) throws Exception {
        System.out.println("传进来的参数"+num);
        if(num >= 2){
            throw new Exception("测试异常");
        }
        return new ModelAndView("index");
    }
    @ApiOperation("请求全部")
    @RequestMapping(value = "/getinfo")
    @ResponseBody
    public Map getuserinfo(String beforeTime,String afterTime,String status,int page,int limit)  {
        if(beforeTime.equals("")){
            beforeTime = null;
        }
        if(afterTime.equals("")){
            afterTime = null;
        }
        show s = new show();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        if(beforeTime != null && afterTime != null){
            try {
                Date AfterTimeparse = sdf.parse(afterTime);
                Date BeforeTimeparse = sdf.parse(beforeTime);
                s.setAfterTime(AfterTimeparse);
                s.setBeforeTime(BeforeTimeparse);
            }catch (ParseException e){
                System.err.println("时间转换异常");
            }
        }else if (beforeTime != null){
            Date BeforeTimeparse = null;
            try {
                BeforeTimeparse = sdf.parse(beforeTime);
            } catch (ParseException e) {
                System.err.println("beforeTime时间转换异常");
            }
            s.setBeforeTime(BeforeTimeparse);
        }else if (afterTime != null){
            Date AfterTimeparse = null;
            try {
                AfterTimeparse = sdf.parse(afterTime);
            } catch (ParseException e) {
                System.err.println("afterTime时间转换异常");
            }
            s.setAfterTime(AfterTimeparse);
        }

        s.setStatus(status);
        s.setPage(((page-1) * limit));
        s.setLimit(limit);
        System.out.println(s);
        List<ex> getalluser = testService.getalluser(s);
        Integer getallusertatoal = testService.getallusertatoal(s);
        System.out.println("查出来的总数"+getallusertatoal);

        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        //查出来的总共条数
        map.put("count", getallusertatoal);
        map.put("data", getalluser);

        return map;
    }


}
